package com.gitee.aurora.job;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.aurora.client.WebSocketClent;

import com.gitee.aurora.agent.dao.ZfjdCheckDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: jiangfuxiao
 * Date:2022/9/7 13:23
 */
@Component
@EnableAsync
@EnableScheduling
public class AgentTaskJob {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WebSocketClent webSocketClent;

    @Resource(name = "zfjdCheckDaoImpl")
    private ZfjdCheckDao zfjdDao;

    @Value("${deptCode}")
    private String deptCode;

    //定时发送Socket消息任务
    @Scheduled(fixedDelay = 60000 * 30)
    public void scheduled() {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> result = new HashMap<>();
            List<String> tableNameList = new ArrayList<>();
            HashMap<String, String> agentTableName = new HashMap<>();
            zfjdDao.queryTable().forEach(v -> {
                tableNameList.add(String.valueOf(v.get("tabCode")));
                agentTableName.put(String.valueOf(v.get("tabCode")), String.valueOf(v.get("tabName")));
            });
            result.put("code", deptCode);
            List<Map<String, String>> tableDataLastUpdateTime = getTableDataLastUpdateTime(tableNameList, agentTableName);
            result.put("message", tableDataLastUpdateTime);
            webSocketClent.sendMessage(mapper.writeValueAsString(result));
            logger.error("定时任务执行 数据中心已发送socket请求");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private List<Map<String, String>> getTableDataLastUpdateTime(List<String> tableNameList, Map<String, String> agentTableNameMap) {
        List<Map<String, String>> resultMsg = new ArrayList<>();
        for (String tableName : tableNameList) {
            Map<String, String> msg = new HashMap<>();
            try {
                msg.put(tableName, zfjdDao.queryTableDataByCode(tableName).get(0));
            }catch (Exception e){
                //如果获取sql语句异常 说明该办案区没有配置sql或者该表数据没有更新时间
                e.printStackTrace();
                msg.put(tableName,"00-00-00 00:00:00");
                msg.put("tableName", agentTableNameMap.get(tableName).split(",")[1]);
                resultMsg.add(msg);
                continue;
            }
            msg.put("tableName", agentTableNameMap.get(tableName));
            resultMsg.add(msg);
        }
        return resultMsg;
    }
}
